﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using r2mp_DataStructures;
using System.Windows.Forms;
using System.Drawing;

namespace r2mp_GUI
{

    /// <summary>
    /// This class is not used anymore and is replaced by r2mpGUI.PSPWorker
    /// </summary>
    class PSPWorker_Deprecated
    {
        Task root;
        float xroot = 50;
        float x = 50;
        float yroot = 10;
        float y = 10;
        float h = 50;
        float w = 150;
        float anz;
        float span = 0;
        float xabstand = 50;
        float yabstand = 10;
        float eZahl = 0;
        float maxDepth = 0;
        float maxAmount = 0;
        bool changed = true;
        float width = 0;
        float height = 0;
        float diffWidth = 0;
        float diffHeight = 0;
        float indexWidth = 0;
        float indexHeight = 0;
        Form form;
        Panel panel;


        float maxWidth = 0;
     

        public PSPWorker_Deprecated(Task tasklist, Form form, Panel panel)
        {
            this.root = tasklist;
            this.form = form;
            this.panel = panel;
            Read();
        }

        public void Read()
        {
            anz = root.Tasks.Count;
            for (int i = 0; i < anz; i++)
            {
                span = span + w;
                span = span + xabstand;
            }
        }


        public void getMaxWidth()
        {
            anz = root.Tasks.Count();
            w = this.Width / (anz + xabstand); 

        }

        public void getMaxAmount(Task now)
        {
            foreach (Task temp in now.Tasks)
            {
                getMaxAmount(temp);
            }
            maxAmount += 1;
        }


        
        public float getMaxDepth(Task now)
        {
            float re = 1;
            float max = 0;
            float tempd;
            if (now.Tasks.Count > 0)
            {
                foreach (Task temp in now.Tasks)
                {

                    tempd = getMaxDepth(temp);
                    if (max < tempd)
                    {
                        max = tempd;
                    }
                }
            }
            else
            {
                return 1;
            }
            return max + 1;


        }
         

       

        public void doPSP(Graphics g)
        {
            float temp = 0;
            if (changed)
            {
                maxAmount = 0;


                for (int i = 0; i < root.Tasks.Count(); i++)
                {
                    getMaxAmount(root.Tasks[i]);
                    if (maxAmount > temp)
                    {
                        temp = maxAmount;
                    }
                    maxAmount = 0;
                }
                maxAmount = temp;
                temp = 0;
                /*foreach (Task now in root.Tasks)
                {
                    if (temp < getMaxDepth(now))
                    {
                        temp = getMaxDepth(now);
                    }
                }*/
                temp = 0;
                foreach (Task now in root.Tasks)
                {
                    temp += getMaxDepth(now);
                }

                maxWidth = temp;


                changed = false;


                width = root.Tasks.Count * (w + xabstand);
                height = maxAmount * (h + yabstand);






                diffWidth = (width - this.Width);
                indexWidth = (width + 100) / this.Width;
                diffHeight = (height - panel.Height);
                indexHeight = (height + 100) / panel.Height;

                if (width > this.Width && height > this.Height)
                {
                    indexHeight = (indexHeight + indexWidth) / 2;
                }
                else if (width > this.Width)
                {
                    indexHeight = (width + 100) / this.Width;
                }
                else if (height > this.Height)
                {
                    indexHeight = (height + 100) / panel.Height;
                }
            }
            xroot = 50 / indexHeight;
            x = 50 / indexHeight;
            yroot = 10 / indexHeight;
            y = 10 / indexHeight;
            h = 50 / indexHeight;
            w = 150 / indexHeight;

            span = 0;
            xabstand = 50 / indexHeight;
            yabstand = 10 / indexHeight;
            eZahl = 0;

            Font fontNummer = new Font("Arial", 10 / indexHeight, FontStyle.Bold);
            Font fontBez = new Font("Arial", 8 / indexHeight, FontStyle.Bold);
            Font fontBezRoot = new Font("Arial", 14 / indexHeight);
            Font fontBezFirst = new Font("Arial", 10 / indexHeight, FontStyle.Bold);

            float mid = x + ((anz * (w + xabstand)) / 2) - (w + xabstand) / 2;

            Pen pen = new Pen(Brushes.Black);
            Pen penFett = new Pen(Brushes.Black, 2);
            Pen marker = new Pen(Brushes.Gray);
            Pen test = new Pen(Brushes.Red);



            //--
            // ROOT--------------------------------------------------------------------------------------------
            //--
            g.FillRectangle(Brushes.LightBlue, mid, y, w, h);
            if (root.Duration == 0)
            {
                g.DrawRectangle(penFett, mid, y, w, h);

            }
            else
            {
                g.DrawRectangle(pen, mid, y, w, h);
            }

            float stringstart = 0;


            if (root.Status == 1)
            {
                g.DrawLine(marker, new PointF(mid, (y + h)), new PointF((mid + w), y));
            }
            else if (root.Status == 2)
            {
                g.DrawLine(marker, new PointF(mid, (y + h)), new PointF((mid + w), y));
                g.DrawLine(marker, new PointF(mid, y), new PointF((mid + w), (y + h)));
            }

            //stringstart = stringwidth(root.Nr.ToString(), fontNummer, e);
            //g.DrawString(root.Nr.ToString(), fontNummer, Brushes.Black, mid + stringstart, y + 8);
            stringstart = stringwidth(root.Name.ToString(), fontBezRoot, g);
            g.DrawString(root.Name.ToString(), fontBezRoot, Brushes.Black, mid + stringstart, y + 15 / indexHeight);
            
            g.DrawLine(penFett, new PointF((mid + (w / 2)), (y + h)), new PointF((mid + (w / 2)), (y + h + yabstand / 2)));

            y = y + h + yabstand;

            //--
            // 1. EBENE--------------------------------------------------------------------------------------
            //--
            for (int a = 0; a < root.Tasks.Count; a++)
            {
                g.FillRectangle(Brushes.AliceBlue, x, y, w, h);
                if (root.Tasks[a].Duration == 0)
                {
                    g.DrawRectangle(penFett, x, y, w, h);
                }
                else
                {
                    g.DrawRectangle(pen, x, y, w, h);
                }

                if (root.Tasks[a].Status == 1)
                {
                    g.DrawLine(marker, new PointF(x, (y + h)), new PointF((x + w), y));
                }
                else if (root.Tasks[a].Status == 2)
                {
                    g.DrawLine(marker, new PointF(x, (y + h)), new PointF((x + w), y));
                    g.DrawLine(marker, new PointF(x, y), new PointF((x + w), (y + h)));
                }
                
                stringstart = stringwidth(root.Tasks[a].Nr.ToString(), fontNummer, g);
                g.DrawString(root.Tasks[a].Nr.ToString(), fontNummer, Brushes.Black, x + stringstart, y + 8 / indexHeight);
                stringstart = stringwidth(root.Tasks[a].Name.ToString(), fontBezFirst, g);
                g.DrawString(root.Tasks[a].Name.ToString(), fontBezFirst, Brushes.Black, x + stringstart, y + 25 / indexHeight);
                
                g.DrawLine(penFett, new PointF((x + w / 2), y), new PointF((x + w / 2), y - yabstand / 2));


                //--
                // 2. EBENE--------------------------------------------------------------------------
                //--
                float by = y;
                for (int b = 0; b < root.Tasks[a].Tasks.Count; b++)
                {
                    eZahl = 10 / indexHeight;
                    paintPSP((Task)root.Tasks[a].Tasks[b], x, y, by, yabstand, w, h, eZahl, g);
                    by = by + h + yabstand;
                    g.DrawLine(penFett, new PointF(x + eZahl, ((by + h + 2 / indexHeight) - (h / 2))), new PointF(x + 1, ((by + h + 2 / indexHeight) - (h / 2))));
                    //--
                    // 3. EBENE-----------------------------------------------------------------------
                    //--
                    float cy = by;
                    for (int c = 0; c < root.Tasks[a].Tasks[b].Tasks.Count; c++)
                    {
                        eZahl = 20 / indexHeight;
                        paintPSP((Task)root.Tasks[a].Tasks[b].Tasks[c], x, y, by, yabstand, w, h, eZahl, g);
                        by = by + h + yabstand;
                        cy = cy + h + yabstand;
                        g.DrawLine(penFett, new PointF(x + eZahl - 10 / indexHeight, ((by + h + 2 / indexHeight) - (h / 2))), new PointF(x + eZahl, ((by + h + 2 / indexHeight) - (h / 2))));
                        //--
                        // 4. EBENE--------------------------------------------------------------------
                        //--
                        float dy = cy;
                        for (int d = 0; d < root.Tasks[a].Tasks[b].Tasks[c].Tasks.Count; d++)
                        {
                            eZahl = 30 / indexHeight;
                            paintPSP((Task)root.Tasks[a].Tasks[b].Tasks[c].Tasks[d], x, y, by, yabstand, w, h, eZahl, g);
                            by = by + h + yabstand;
                            dy = dy + h + yabstand;
                            g.DrawLine(penFett, new PointF(x + eZahl - 10 / indexHeight, ((by + h + 2 / indexHeight) - (h / 2))), new PointF(x + eZahl, ((by + h + 2 / indexHeight) - (h / 2))));
                            //--
                            // 5. EBENE----------------------------------------------------------------
                            //--
                            float ey = dy;
                            for (int f = 0; f < root.Tasks[a].Tasks[b].Tasks[c].Tasks[d].Tasks.Count; f++)
                            {
                                eZahl = 40 / indexHeight;
                                paintPSP((Task)root.Tasks[a].Tasks[b].Tasks[c].Tasks[d].Tasks[f], x, y, by, yabstand, w, h, eZahl, g);
                                by = by + h + yabstand;
                                ey = ey + h + yabstand;
                                g.DrawLine(penFett, new PointF(x + eZahl - 10 / indexHeight, ((by + h + 2 / indexHeight) - (h / 2))), new PointF(x + eZahl, ((by + h + 2 / indexHeight) - (h / 2))));
                                //-------------------------------------------

                                g.DrawLine(penFett, new PointF(x + 31 / indexHeight, dy + h), new PointF(x + 31 / indexHeight, ey + h / 2 + 2));
                            }

                            g.DrawLine(penFett, new PointF(x + 21 / indexHeight, cy + h), new PointF(x + 21 / indexHeight, dy + h / 2 + yabstand + h + 2));
                        }

                        g.DrawLine(penFett, new PointF(x + 11 / indexHeight, by + h / 2 + 2), new PointF(x + 11 / indexHeight, cy - yabstand));

                    }
                }

                if (root.Tasks[a].Tasks.Count > 0)
                {
                    g.DrawLine(penFett, new PointF(x + 1, y + h), new PointF(x + 1, by + h / 2 + 2));
                }

                x = x + w + xabstand;
            }
            g.DrawLine(penFett, new PointF((xroot + w / 2), (yroot + h + yabstand) - yabstand / 2), new PointF(xroot + ((w + xabstand) * (anz - 1)) + w / 2, (yroot + h + yabstand) - yabstand / 2));
        }



        public void paintPSP(Task task, float x, float y, float by, float yabstand, float w, float h, float eZ, Graphics g)
        {
            Font fontNummer = new Font("Arial", 10 / indexHeight, FontStyle.Bold);
            Font fontBez = new Font("Arial", 8 / indexHeight, FontStyle.Bold);
            Pen pen = new Pen(Brushes.Black);
            Pen penFett = new Pen(Brushes.Black, 2);
            Pen marker = new Pen(Brushes.Gray);
            float stringstart = 0;
            float ebenenZahl = eZ;
            g.FillRectangle(Brushes.AliceBlue, x + ebenenZahl, by + h + yabstand, w, h);
            if (task.Duration == 0)
            {
                g.DrawRectangle(penFett, x + ebenenZahl, by + h + yabstand, w, h);
            }
            else
            {
                g.DrawRectangle(pen, x + ebenenZahl, by + h + yabstand, w, h);
            }
            if (task.Status == 1)
            {
                g.DrawLine(marker, new PointF(x + ebenenZahl, by + h + yabstand + h), new PointF(x + ebenenZahl + w, by + h + yabstand));
            }
            else if (task.Status == 2)
            {
                g.DrawLine(marker, new PointF(x + ebenenZahl, by + h + yabstand + h), new PointF(x + ebenenZahl + w, by + h + yabstand));
                g.DrawLine(marker, new PointF(x + ebenenZahl, by + h + yabstand), new PointF(x + ebenenZahl + w, by + h + yabstand + h));
            }
            stringstart = stringwidth(task.Nr.ToString(), fontNummer, g);
            g.DrawString(task.Nr.ToString(), fontNummer, Brushes.Black, x + stringstart + ebenenZahl, by + h + yabstand + 8 / indexHeight);
            stringstart = stringwidth(task.Name.ToString(), fontBez, g);
            g.DrawString(task.Name.ToString(), fontBez, Brushes.Black, x + stringstart + ebenenZahl, by + h + yabstand + 25 / indexHeight);
        }



        public float stringwidth(string str, Font font, Graphics g)
        {
            float width;
            SizeF stringSize = new SizeF();
            stringSize = g.MeasureString(str, font);
            width = (float)stringSize.Width;
            width = (w - width) / 2;
            return width;
        }

        //private void panel1_MouseDown(object sender, MouseEventArgs e)
        //{
        //    float mausX = (float)e.X;
        //    float mausY = (float)e.Y;
        //}

        public float Width 
        {
            get
            {
                return form.Width;
            }
        }

        public float Height
        {
            get
            {
                return form.Height;
            }
        }
    }
}
